package com.innova_t.android.mario;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.restlet.resource.ClientResource;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.innova_t.android.mario.items.ListoItemCheckoutActivity;
import com.spark.listoh.ext.models.ListDTO;
import com.spark.listoh.ext.models.ListItemDTO;
import com.spark.listoh.ext.models.ListoClientResource;
import com.spark.listoh.ext.models.ListoItemClientResource;

public class MobilistestActivity extends ListActivity {
    
	private static final String TAG = "Listoh";
	private static final int ACTIVITY_EDIT = 0;
	private ItemsDbAdapter mDbHelper;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.i(TAG, "before starting the db"  );
        
        mDbHelper = new ItemsDbAdapter(this);
        
        
        // now fill data
        ClientResource cr = 
        		new ClientResource("http://beta.list-oh.com/rest/lists");
        
        ListoClientResource clientRes = 
        		(ListoClientResource) cr.wrap(ListoClientResource.class);
        
        Log.i(TAG, "client object created");
        
//        String status = clientRes.getStatus("ok");
        
//        Log.i(TAG, "status: " + status );
        
        List<ListDTO> lista = clientRes.getLists();
        
        if( lista == null ){
        	Log.i(TAG, "there are no lists available");
        	return;
        }
        
        setListAdapter( new ListoListAdapter(lista) );
        
        
    }
    
    
    @Override
	protected void onListItemClick(ListView parent, View v, int position, long id) {
//		super.onListItemClick(parent, v, position, id);
		
		// get adpter
    	ListoListAdapter adptr = (ListoListAdapter)  parent.getAdapter();
		
    	ListDTO listoLista = (ListDTO) adptr.getItem(position);
		//contact.msgCount++;
		
		// pasamos a items y guardamos a bbdd
//		adptr.notifyDataSetInvalidated();
		
		// set next page
		String urlForItems = "/lists/" + Long.valueOf(listoLista.getId() )
				+ "/items";
		
		Log.i(TAG, "before initializing: " + urlForItems );
		
		ClientResource cr = 
        		new ClientResource("http://beta.list-oh.com/rest" + urlForItems);
		
		ListoItemClientResource clientRes = 
        		(ListoItemClientResource) cr.wrap(ListoItemClientResource.class);
        
        Log.i(TAG, "client listoitem object created");
        
        ArrayList<ListItemDTO> listItems = clientRes.getItemsByListId();
        
        // now insert them to the database
        
        Iterator<ListItemDTO> it = listItems.iterator();
        mDbHelper.open();
        while( it.hasNext() ){
        	// insert each item
        	ListItemDTO itemLoc = it.next();
        	
        	Log.i(TAG, "adding title: " + itemLoc.getTitle() );
        	
        	mDbHelper.createItem(""+listoLista.getId(),
        			""+ itemLoc.getId() ,
        			"" +itemLoc.isPostPonned(), 
        			itemLoc.getScheduledDate().toString(), 
        			itemLoc.getScheduledDate().toString(), 
        			itemLoc.getDescription(), 
        			itemLoc.getScheduleType(),
        			itemLoc.getTitle());
        }
        mDbHelper.close();
        Log.i(TAG, "all items done"  );
        
        Intent i = new Intent(this, ListoItemCheckoutActivity.class);
        i.putExtra(ItemsDbAdapter.KEY_ROWID, listoLista.getId() );
        i.putExtra(ListoItemCheckoutActivity.REST_URL, urlForItems );
        startActivityForResult(i, ACTIVITY_EDIT);
        
	}
}